<?php
namespace app\api\controller;

use library\Controller;
use think\facade\Config;
use think\Db;

class Queue extends Controller{
	
	
	//检测是否进入公海（每天执行一次）
    public function opensea_change()
    {
    	//进入公海
		set_time_limit(0);
		$today = date('Y-m-d',time());
		$create_at = date('Y-m-d H:i:s',time());
		$where[] = ['is_deleted','eq',0];
		$where[] = ['status','eq',1];
    	$where[] = ['charger','neq',0];
    	$where[] = ['end_at','<',$today];
    	
    	$list = Db::name('system_customer')->where($where)->field('id,charger')->select();
        if($list){
        	$nickname = Db::name('system_user')->where(['id'=>10000])->value('nickname');
        	$nickname = $nickname ? $nickname : '超级管理员';
        	foreach($list as $k=>$v){
        		Db::startTrans();
		        try {           	
				    $end_at = get_futuredate(get_customer_config(1,'expire_day'));        
		            Db::name('system_customer')->where(['id'=>$v['id']])->update(['status'=>2,'charger'=>0,'last_sea_at'=>$today]);
		            
		            //写入客户转移记录表				
					$data = [
						'customer_id' => $v['id'],
						'charger' => $v['charger'],
						'type' => 3,
						'describe'=>'系统收回进入公海',
						'add_at' => $today,
						'created_at'=>$create_at,
						'updated_at'=>$create_at,
					];
					Db::name('system_customer_shifts')->insert($data);
					
		            Db::commit();
					operatelog_queue($v['id'],'SystemCustomer','系统收回进入公海',10000,$nickname);
		            continue;
		        } catch (Exception $e) {
		            Db::rollback();
		            continue;
		        }
        	}
        }
    }
	
	
   
    
}